package com.scap.authen.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scap.authen.entity.Menu;
import com.scap.authen.entity.Role;
import com.scap.authen.service.MenuMappingService;
import com.scap.authen.service.MenuService;
import com.scap.authen.service.RoleAppService;
import com.scap.authen.service.RoleService;
import com.scap.authen.util.DataTable;

@Controller
@RequestMapping(value = "/roleMenu")
public class RoleMenuMappingController {	
	@Autowired
	MenuService voMenuService;
	@Autowired
	RoleService voRoleService;
	@Autowired
	private RoleAppService voRoleAppService;
	@Autowired
	private ApplicationContext context;
	@Autowired
	MenuMappingService voMenuMappingService;	
	DataTable voDataTable = new DataTable();
	
	
	@RequestMapping( method = RequestMethod.GET)
	public String rolePage ( HttpServletRequest request ,ModelMap model, 
			@RequestParam String roleId, 
			@RequestParam String siteId,
//			@RequestParam String vaAppId,
			@RequestParam String roleName) {
		//find Application checked
//		String vaMenuChecked = "";
//		vaMenuChecked = voMenuMappingService.findMenuChecked(vaAppId, roleId, siteId);	
		List<HashMap<String, String>> applicationsList = voRoleAppService.findApplicationByRoleIdSiteId(roleId, siteId);
		model.addAttribute("roleMenu",new Menu());
		model.addAttribute("applicationList",applicationsList);
		model.addAttribute("page", "Role Menu Mapping ");
//		model.addAttribute("vaAppChecked", vaMenuChecked);
		model.addAttribute("vaRoleId", roleId);
		model.addAttribute("vaSiteId", siteId);
		model.addAttribute("vaRoleName", roleName);
		return "Role/roleMenuMapping";
	} 	

	@RequestMapping(value = "/roleMenuDataTables", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
	public @ResponseBody String springPaginationDataTables(HttpServletRequest request) throws IOException {	
		String vaAppId = request.getParameter("vaAppId");
		//find all Application
		List<Menu> roleMenuList = voMenuService.getMenuByApplicationId(vaAppId);
		return voDataTable.springPaginationDataTables(request,roleMenuList);
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String roleApplicationMappingManagement ( @Valid Role role ,BindingResult result, HttpServletRequest request, ModelMap model ) {		
		String vaMessage = "";	
		String vaSiteId = request.getParameter("vaSiteId");
		String vaRoleId = request.getParameter("vaRoleId");
		String vaMenuId = request.getParameter("paMenuIdChecked");
		String vaApplicationId = request.getParameter("vaAppId");
		// update role status
		voRoleService.updateMenuStatus(vaRoleId, vaSiteId);	
		//Delete MenuMapping by ApplicationId,SiteId,RoleId
		voMenuMappingService.deleteMenuMapping(vaSiteId, vaRoleId, vaApplicationId);
		// Insert MenuMapping
		vaMessage = voMenuMappingService.saveMenuMapping(vaSiteId, vaRoleId, vaApplicationId, vaMenuId, request);
		model.addAttribute("vaMessage",vaMessage);
		return  "Role/roleManagement";
	}
}
